﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Xml.XPath;
using Songhay.Data.GenericWeb.Properties;
using Songhay.Xml;

namespace Songhay.Data.GenericWeb.Procedure
{
    /// <summary>
    /// List procedures for Web-schema data.
    /// </summary>
    internal static partial class List
    {
        internal static XPathDocument SearchForEdit(DbConnection commonConnection, IXPathNavigable navigableInput)
        {
            XPathNavigator n = XmlUtility.GetNavigableNode(navigableInput, "//SearchText");
            if(n == null) throw new Exception("SearchText node not found.");

            string s = string.Format("%{0}%", n.Value);

            if(commonConnection.State == ConnectionState.Open)
            {
                Dictionary<string, object> param = new Dictionary<string, object>(1);
                param.Add("@Search", s);
                s = Common.CommonScalar.GetString(commonConnection, Resources.Search, param);
            }

            if(string.IsNullOrEmpty(s))
            {
                return XmlUtility.GetInternalMessageDocument("Your Search Returned No Results", new string[] { string.Format("<strong>Search:</strong> <em>{0}</em>", n.Value) });
            }
            else return XmlUtility.GetNavigableDocument(s);
        }
    }
}
